Overloading and overriding method call

Categories: Java; Tagged with: ; @ May 15th, 2012 21:11

Overloading method will be determined by reference type, but override method will be determined by the actual object tyep.
property is determined by reference type too.

package newjob.guoliang.corejava;

public class ReferenceTypeTest {
	public static void main(String[] args) {
		AClass a = new BClass();
		new ReferenceTypeTest().printClassDesc(a);
	}
	
	protected void printClassDesc(AClass a) {
		System.out.println("A_" + a.desc);
	}
	
	protected void printClassDesc(BClass b) {
		System.out.println("B_" + b.desc);
	}
	
}

class AClass {
	String desc = "ClassA";
}

class BClass extends AClass{
	String desc = "ClassB";
}

Output is:

A_ClassA
MethodClassB_ClassB

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.